//
//  RecommendViewController.swift
//  DouYuSwift
//
//  Created by dingjunchuxing on 2019/7/4.
//  Copyright © 2019 text. All rights reserved.
//

import UIKit


private let kMarginSpace : CGFloat = 10.0
private let kItemW = (kScreenW - 3 * kMarginSpace) / 2
private let kNormalItemH = kItemW * 3/4
private let kPrettyItemH = kItemW * 4/3
private let kSectionHeaderH : CGFloat =  50

private let kNormalCellID = "kNormalCellID"
private let kPrettyCellID = "kPrettyCellID"
private let kHeaderllID = "kHeaderllID"

class RecommendViewController: UIViewController {

    //懒加载
    private lazy var collectionView : UICollectionView = { [unowned self] in
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = kMarginSpace
        layout.itemSize = CGSize(width: kItemW, height: kNormalItemH)
        layout.sectionInset = UIEdgeInsets(top: 0, left: kMarginSpace, bottom: 0, right: kMarginSpace)
        layout.headerReferenceSize = CGSize(width: kScreenW, height: kSectionHeaderH)
        
        
        
        let collectView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectView.dataSource = self
        collectView.delegate = self
        collectView.autoresizingMask = [.flexibleHeight]
        collectView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kNormalCellID)
        collectView.register(UINib(nibName: "CollectionViewNormalCell", bundle: nil), forCellWithReuseIdentifier:kNormalCellID)
        collectView.register(UINib(nibName: "CollectionViewPrettyCell", bundle: nil), forCellWithReuseIdentifier:kPrettyCellID)
        
        collectView.register(UINib(nibName: "RecommendHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kHeaderllID)
        collectView.backgroundColor = UIColor.white
        
        return collectView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
}

extension RecommendViewController{
    
    func setupUI(){
        view.addSubview(collectionView)
    }
}

extension RecommendViewController:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return 8
        }
        
        return 4
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 12
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       
        var cell : UICollectionViewCell
        if indexPath.section == 1 {
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: kPrettyCellID, for: indexPath)
        }else{
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNormalCellID, for: indexPath)
        }
    
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kHeaderllID, for: indexPath)
        headerView.backgroundColor = UIColor.green
        
        return headerView
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: kItemW, height: kPrettyItemH)
        }else{
             return CGSize(width: kItemW, height: kNormalItemH)
        }
    }
}
